home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / net / netamsrc.arc / amiga_pt.c next >
Encoding:
C/C++ Source or Header  |  1988-02-12  |  2.4 KB  |  121 lines

  1. #include "global.h"
  2.  
  3. extern char Amiga_drive[];
  4.  
  5. /* Given a working directory and an arbitrary pathname, resolve them into
  6.  * an absolute pathname. Memory is allocated for the result, which
  7.  * the caller must free
  8.  */
  9. char *
  10. pathname(cd,path)
  11. char *cd;    /* Current working directory */
  12. char *path;    /* Pathname argument */
  13. {
  14.     register char *buf,*cp;
  15.     char *cdtmp,*pathtmp;
  16.  
  17.     if(cd == NULLCHAR || path == NULLCHAR)
  18.         return NULLCHAR;
  19.     /* Strip any leading white space on args */
  20.     while(*cd == ' ' || *cd == '\t')
  21.         cd++;
  22.     while(*path == ' ' || *path == '\t')
  23.         path++;
  24.  
  25.     /* Allocate and initialize output buffer; user must free */
  26.     buf = malloc((unsigned)strlen(Amiga_drive) +
  27.         (unsigned)strlen(cd) + strlen(path) + 10);    /* fudge factor */
  28. /*
  29.     sprintf(buf, "%s", Amiga_drive);
  30. */
  31.     /* Interpret path relative to cd only if it doesn't begin with "/" */
  32.     if(path[0] != ':')
  33.         crunch(buf, cd);
  34.  
  35.     crunch(buf,path);
  36.  
  37.     /* Special case: null final path means the root directory */
  38.     if(buf[0] == '\0'){
  39.         buf[0] = ':';
  40.         buf[1] = '\0';
  41.     }
  42.  
  43.     return buf;
  44. }
  45.  
  46. /* Process a path name string, starting with and adding to
  47.  * the existing buffer
  48.  */
  49. static
  50. crunch(buf,path)
  51. char *buf;
  52. register char *path;
  53. {
  54.     register char *cp;
  55.     
  56.  
  57.     cp = buf + strlen(buf);    /* Start write at end of current buffer */
  58.     
  59.     /* Now start crunching the pathname argument */
  60.     /* Strip leading /'s; one will be written later */
  61.     
  62.     if (*path == ':' )
  63.     {
  64.         cp = buf;
  65.         *cp++ = ':';
  66.         path++;
  67.     }
  68.     while ( *path == '/' )
  69.     {
  70.         /* Hop up a level */
  71.         if((cp = rindex(buf,'/')) == NULLCHAR)
  72.         {
  73.             cp = buf;    /* Don't back up beyond root */
  74.             *cp++ = ':';
  75.         }
  76.         *cp = '\0';        /* In case there's another / */
  77.         path++;        /* Skip "/" */
  78.     }
  79.     
  80.     if(*path == '\0')
  81.     {
  82.         *cp++ = '\0';        /* no more, all done */
  83.         return;
  84.     }
  85.     /* Look for parent directory references, either at the end
  86.      * of the path or imbedded in it
  87.      */
  88.     for(;;)
  89.     {
  90.         if(*path == '\0')
  91.         {
  92.             break;        /* no more, all done */
  93.         }
  94.         if(strcmp(path,"//") == 0 )
  95.         {
  96.             /* Hop up a level */
  97.             if((cp = rindex(buf,'/')) == NULLCHAR)
  98.                 cp = buf;    /* Don't back up beyond root */
  99.             *cp = '\0';        /* In case there's another .. */
  100.             path += 2;        /* Skip ".." */
  101.             while(*path == '/')    /* Skip one or more slashes */
  102.             {
  103.                 path++;
  104.             }
  105.         }
  106.         else 
  107.         {
  108.             if ( *path == '/' )
  109.                 path++;
  110.             /* Ordinary name, copy up to next '/' or end of path */
  111.             if ( *(cp-1) != ':' )
  112.             {
  113.                 *cp++ = '/';
  114.             }
  115.             while(*path != '/' && *path != '\0')
  116.                 *cp++ = *path++;
  117.         }
  118.     }
  119.     *cp++ = '\0';
  120. }
  121.